
$(document).ready(function(){  

	
	
	

	
	$('#submit-pinnwand-org').click(function(e){
		
		
		
		function getHtml(arrdata){
			var id = arrdata.id;
			var created = arrdata.createdat;
			var html = '<div class="row pinnwand-kommentar">';
			html += '<div class="col-md-2">';
			html += '<img class="img-responsive" src="/' + path + 'pinnwand/' + imagest + '">';				
			html += '</div>';
			html += '<div class="col-md-10"><span>' + username + ' hat am ' + created + ' geschrieben</span><br/>' + $("textarea[name='pinnwand']").val() + '</div>';
			html += '</div>';
			html += '<div class="row pinnwand-texarea">';
			html += '<p style="margin-bottom:1px;">';
			html += '<textarea id="kommentar_' + id + '" class="form-control" placeholder="Dein Kommentar dazu" rows="1" name="pinnwand"></textarea>';
			html += '</p>';
			html += '<p>';
			html += '<button id="btn_' + id + '" class="btn btn-primary submit-kommentar" type="submit" style="padding:3px 6px; font-size:12px;">Kommentar</button>';
			html += '<span id="aj-set-image_' + id + '" style="display:none;">';
			html += '<img src="/bundles/acmeperson/images/ajax-loader.gif">';
			html += '</span>';
			html += '</p>';
			html += '</div>';			
			return html;
		}
		
		
		
		e.preventDefault();
		var pathArray = window.location.href.split('/');
		protocol = pathArray[0];
		host = pathArray[2];
		url = protocol + '//' + host + $( "input[name='url-pinnwand']").val();
		
		$("#aj-set-image").show();
		
		$.post( url , { pinnwand: $("textarea[name='pinnwand']").val(), groupid: 0 }, 
				function(arrdata){
					if(arrdata != ""){
						arrdata = jQuery.parseJSON(arrdata);
						$(".pinnwand h1").next().before($(getHtml(arrdata)).fadeIn());
						$("textarea[name='pinnwand']").val("");

					}
					$("#aj-set-image").hide();
				}
		);
		
	});
	

	$('#submit-pinnwand').click(function(e){
		
		e.preventDefault();
		var pathArray = window.location.href.split('/');
		protocol = pathArray[0];
		host = pathArray[2];
		url = protocol + '//' + host + $( "input[name='url-pinnwand']").val();
		
		$("#aj-set-image").show();
		
		$.post( url , { pinnwand: $("textarea[name='pinnwand']").val(), groupid: 0 }, 
				function(arrdata){
					if(arrdata != ""){
						arrdata = jQuery.parseJSON(arrdata);
						url = protocol + '//' + host + $( "input[name='url-index']").val();
						$(".pinnwand").load(url + " .pinnwand > *", function(){
							$("#aj-set-image").hide();
							$("textarea[name='pinnwand']").val("");							
						});
						
						// send Email
						url = protocol + '//' + host + $( "input[name='url-send-pinnwand']").val() + '/' + arrdata['id'];
						$.get( url );
					}

				}
		);
		
	});
	
	
	$(document).on("click", ".submit-kommentar", function(){
		
		var arr_id = $(this).attr("id").split("_");
		
		var pathArray = window.location.href.split('/');
		protocol = pathArray[0];
		host = pathArray[2];
		url = protocol + '//' + host + $( "input[name='url-pinnwand']").val();
		
		$("#aj-set-image_" + arr_id[1]).show();
		$.post( url , { pinnwand: $("#kommentar_" + arr_id[1]).val(), groupid: arr_id[1] }, 
				function(arrdata){
					arrdata = jQuery.parseJSON(arrdata);
					url = protocol + '//' + host + $( "input[name='url-index']").val();
					$(".pinnwand").load(url + " .pinnwand > *", function(){
						$("#aj-set-image_" + arr_id[1]).hide();
						$("#kommentar_" + arr_id[1]).val("");
						$(".pinnwand").fadeIn();
					});
					
					// send Email
					url = protocol + '//' + host + $( "input[name='url-send-pinnwand']").val() + '/' + arrdata['id'];
					$.get( url );
					
					
					
				}
		);
	});
	
	
	
	$(document).on("click", ".submit-kommentar_org", function(){
		
		var arr_id = $(this).attr("id").split("_");

		function getHtml(created){
			var html = '<div class="row pinnwand-kommentar" style="margin-bottom:2px;">';
			html += '<div class="col-md-2">';
			html += '<img class="img-responsive" src="/' + path + 'pinnwand/' + imagest + '">';				
			html += '</div>';
			html += '<div class="col-md-10"><span>' + username + ' hat am ' + created + ' geschrieben</span><br/>' + $("#kommentar_" + arr_id[1]).val() + '</div>';
			html += '</div>';
			return html;
		}
		
		var pathArray = window.location.href.split('/');
		protocol = pathArray[0];
		host = pathArray[2];
		url = protocol + '//' + host + $( "input[name='url-pinnwand']").val();
		
		$("#aj-set-image_" + arr_id[1]).show();
		$.post( url , { pinnwand: $("#kommentar_" + arr_id[1]).val(), groupid: arr_id[1] }, 
				function(arrdata){
					if(arrdata != ""){
						 
						$("#btn_" + arr_id[1]).parent().parent().prev().attr("style","margin-bottom:0px;");
						$("#btn_" + arr_id[1]).parent().parent().before($(getHtml(arrdata.createdat)).fadeIn());
					}
					$("#kommentar_" + arr_id[1]).val("");
					$("#aj-set-image_" + arr_id[1]).hide();
				}
		);
	});
	
	$(document).on("click", ".history", function(){
		$("#aj-history").show();
		var pathArray = window.location.href.split('/');
		protocol = pathArray[0];
		host = pathArray[2];
		url = protocol + '//' + host + $( "input[name='url-index']").val() + '?limitFrom=' + $(this).attr('id');
		$(".pinnwand").load(url + " .pinnwand > *", function(){
			$("#aj-history").hide();
		});
    	return false;
    });
    
	
	
	
	function sendBookEmail(){
		
	}
	
	
});  